﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Modbus;
using FtdAdapter;
using Modbus.Data;
using Modbus.Device;
using iCollect.Constants;
//using Modbus.Utility;

namespace iCollect.CollectDetail
{
    public partial class OnlineCollect2 : UserControl
    {
        public OnlineCollect2()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Simple Modbus serial RTU master write holding registers example.
        /// </summary>
        public void ModbusSerialRtuMasterReadRegisters()
        {
            try
            {
                using (SerialPort port = new SerialPort(GlobalConstants.port))
                {
                    // configure serial port
                    port.BaudRate = 9600;
                    port.DataBits = 8;
                    port.Parity = Parity.None;
                    port.StopBits = StopBits.One;
                    port.Open();

                    // create modbus master
                    IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);

                    byte slaveId = GlobalConstants.slaveID;
                    ushort startAddress;//= 40;
                    ushort numRegisters;// = 5;
                    ushort[] registers;
                    String channelStr = GlobalConstants.channelMap["PIT3699"].ToString();
                    startAddress = (ushort)(int.Parse(channelStr.Substring(2)) - 1);
                    numRegisters = 1;
                    registers = master.ReadInputRegisters(slaveId, startAddress, numRegisters);

                    richTextBox1.Text += "PIT3699 " + GlobalConstants.channelMap["PIT3699"].ToString() + " " + registers[0].ToString() + "\n";

                    startAddress = 64;
                    numRegisters = 15;

                    registers = master.ReadInputRegisters(slaveId, startAddress, numRegisters);

                    richTextBox1.Text += "PT3697 " + "30065 " + registers[65 - startAddress - 1].ToString() + "\n";
                    richTextBox1.Text += "PIT3649 " + "30068 " + registers[68 - startAddress - 1].ToString() + "\n";
                    richTextBox1.Text += "PIT3652 " + "30072 " + registers[72 - startAddress - 1].ToString() + "\n";
                    richTextBox1.Text += "TE3692 " + "30076 " + registers[76 - startAddress - 1].ToString() + "\n";
                    richTextBox1.Text += "TE3629A " + "30077 " + registers[77 - startAddress - 1].ToString() + "\n";
                    richTextBox1.Text += "TE3630A " + "30078 " + registers[78 - startAddress - 1].ToString() + "\n";
                    richTextBox1.Text += "TE3694 " + "30075 " + registers[75 - startAddress - 1].ToString() + "\n";

                    port.Close();
                }
            }
            catch (Exception ex)
            {
                richTextBox1.Text += ex.Message;
            }
            
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            ModbusSerialRtuMasterReadRegisters();
        }
    }
}
